<?php

/**
 * This is the model class for table "tbl_employeenotes".
 *
 * The followings are the available columns in table 'tbl_employeenotes':
 * @property integer $id
 * @property string $empno
 * @property string $dateofnote
 * @property string $note
 * @property string $addedby
 * @property string $dateadded
 */
class Employeenotes extends CActiveRecord
{
    const TYPE_PUBLIC = 0;
    const TYPE_PRIVATE =1;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Employeenotes the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_employeenotes';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('dateofnote, note,type', 'required'),
			array('empno', 'length', 'max'=>4),
			array('addedby,dateadded', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, empno, dateofnote, note, addedby, dateadded', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'employee'=>array(self::BELONGS_TO,'Employees','empno'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'empno' => 'Employee Number',
			'dateofnote' => 'Date Of Note',
			'note' => 'Note',
			'addedby' => 'Added by',
			'dateadded' => 'Date Added',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('empno',$this->empno,true);
		$criteria->compare('dateofnote',$this->dateofnote,true);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('addedby',$this->addedby,true);
		$criteria->compare('dateadded',$this->dateadded,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeSave() {
            
            if($this->isNewRecord){
                $this->dateadded=DateUtil::getDefaultCurrentDateTime();
                $this->addedby=Yii::app()->user->name;
            }
            return parent::beforeSave();
        }
        
        public function getTypeList(){
            return array(
                self::TYPE_PUBLIC=>'Public',
                self::TYPE_PRIVATE=>'Private'
            );
        }
        public function getTypeText(){
            return isset($this->typeList[$this->type])?$this->typeList[$this->type]:null;
        }
}